package com.recruit.server.service.impl;

import com.recruit.server.common.HeaderInfo;
import com.recruit.server.dao.ProductMapper;
import com.recruit.server.entity.pojo.Product;
import com.recruit.server.request.product.ListProductRequest;
import com.recruit.server.request.product.ProductDetailRequest;
import com.recruit.server.response.product.ListProductResponse;
import com.recruit.server.response.product.ProductDetailResponse;
import com.recruit.server.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商品列表
     */
    @Override
    public ListProductResponse list(ListProductRequest request, HeaderInfo headerInfo) {
        ListProductResponse response = new ListProductResponse();
        try {
            // select products from mysql db
            List<Product> productList = productMapper.selectByNameAndCategoryIds(null, null);
            response.setProductList(productList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    /**
     * 通过商品id查询商品记录
     */
    @Override
    public ProductDetailResponse detail(ProductDetailRequest request, HeaderInfo headerInfo) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            Product product = productMapper.selectByPrimaryKey(request.getProductId());
            response.setProduct(product);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }
}
